package com.wl156.adapters;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wl156.R;
import com.wl156.activities.InternalMessage;

public class MessageAdapter extends BaseAdapter {

	LayoutInflater mInflater;
	String[] list;

	public MessageAdapter(Context context, String[] list) {
		mInflater = LayoutInflater.from(context);
		this.list = list;
	}

	@Override
	public int getCount() {
		if (list == null) {
			return 0;
		}
		return list.length;
	}

	@Override
	public String getItem(int position) {
		if (position > list.length - 1) {
			return "";
		}
		return list[position];
	}

	@Override
	public long getItemId(int position) {
		try {
			long id = Long.valueOf(getItem(position).split("##")[0]);
			return id;
		} catch (Exception e) {
		}
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View view = mInflater.inflate(R.layout.item_internal_message, null);
		TextView tv = (TextView) view.findViewById(R.id.title);
		TextView type = (TextView) view.findViewById(R.id.type);
		TextView date = (TextView) view.findViewById(R.id.date);

		try {
			String item = getItem(position);
			String[] line = item.split("##");
			tv.setText(line[2]);
			type.setText(line[1]);
			date.setText(line[3].substring(5, line[3].length() - 3));

			int readed = Integer.valueOf(line[4]);
			switch (readed) {
			case 0:
				view.setBackgroundColor(Color.YELLOW);
				break;
			case 1:
				break;
			}
		} catch (Exception e) {
		}

		return view;
	}
}
